package westh.ilib.struts.action.manage.conference;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.ilib.struts.action.manage.resource.ResourceAddAction;
import westh.util.YyAction;
import westh.util.YyID;
import westh.config.Config;

public class ConferenceAddAction extends YyAction{
	// 定义commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ResourceAddAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 日志和权限检查，请勿删除
		westh.log.YyLog.addFootprint(request, "动作－添加会议");

		DynaActionForm f = (DynaActionForm) form;

		String conferenceName = f.getString("conferenceName");
		String conferenceFullName = f.getString("conferenceFullName");
		String conferenceVenue = f.getString("conferenceVenue");
		String conferenceDateStart = f.getString("conferenceDateStart");
		String conferenceDateEnd = f.getString("conferenceDateEnd");
		String conferenceDeadline = f.getString("conferenceDeadline");
		String conferenceNotification = f.getString("conferenceNotification");
		String conferenceCameraReady = f.getString("conferenceCameraReady");
		String conferenceDateReg=f.getString("conferenceDateReg");
		String conferenceUrl = f.getString("conferenceUrl");
	    //获得编号
	    String strId = YyID.getUUID32();
	    /*************************cdoi info start******************************/
	    String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
	    //形成json进行封装 
	    JSONObject jsonCdoi = new JSONObject(); 
	    jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识
	    jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //注册机构编码
	    jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //语种标识符
	    jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //用户标识符
	    jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //类型标识符
	    jsonCdoi.put("DC_TITLE", conferenceFullName);                                   //题名
	    jsonCdoi.put("DC_CREATOR", conferenceVenue);                                //创建者
	    jsonCdoi.put("DC_SUBJECT", "");                                       //主题
	    jsonCdoi.put("DC_DESCRIPTION", "");                           //描述
	    jsonCdoi.put("DC_PUBLISHER", "");                                     //出版者
	    jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //其他责任者
	    jsonCdoi.put("DC_DATE", conferenceDateStart);                                  //日期
	    jsonCdoi.put("DC_FORMAT", "");                                        //格式
	    jsonCdoi.put("DC_IDENTIFIER", "");                                    //标识符
	    jsonCdoi.put("DC_SOURCE", "");                                        //来源
	    jsonCdoi.put("DC_RELATION", "");                                      //关联
	    jsonCdoi.put("DC_COVERAGE", "");                                      //覆盖范围                         
	    jsonCdoi.put("DC_RIGHTS", "");                                        //权限
	    jsonCdoi.put("CDOI_URL_SOURCE", "");                                  //资源URL
	    jsonCdoi.put("CDOI_STATUS", "1");                                     //资源注册状态，2审核通过
	    jsonCdoi.put("CDOI_NOTE", "");                                        //备注
	  
/*	    String cdoiJson = jsonCdoi.toString();
	    String cdoiId="";
	    try{
	    	MuleClient clientW = new MuleClient();
//			  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
	    	UMOMessage umo = clientW.send("axis:http://localhost:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
	    	cdoiId = (String)umo.getPayload();
	    } catch (Exception e) {
	    	throw new RuntimeException(e);
	    }*/
		/** ****************************cdoi info end************************ */

		/** ****************************cdo info start************************ */
		JSONObject jsonObj = new JSONObject();

		jsonObj.put("conferenceId", rId);                        // conference Id
		//jsonObj.put("conferenceId", cdoiId);                        // conference Id
		jsonObj.put("conferenceName", conferenceName);                  // conference name
		jsonObj.put("conferenceFullName", conferenceFullName);              // conferenceFullName
		jsonObj.put("conferenceVenue", conferenceVenue);              // conferenceVenue
		jsonObj.put("conferenceDateStart", conferenceDateStart);                // conferenceDateStart
		jsonObj.put("conferenceDateEnd", conferenceDateEnd);      // conferenceDateEnd
		jsonObj.put("conferenceDeadline", conferenceDeadline);                    // conferenceDeadline
		jsonObj.put("conferenceNotification", conferenceNotification);                  // conferenceNotification
		jsonObj.put("conferenceCameraReady", conferenceCameraReady);                      // conferenceCameraReady
		jsonObj.put("conferenceDateReg", conferenceDateReg);                      // conferenceDateReg
		jsonObj.put("conferenceUrl", conferenceUrl);                      // conferenceDateReg
		String input = jsonObj.toString();
		

		String id = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=addConferenceJson",input, null);
			id = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/** ****************************cdo info end************************ */
		if (id.equals("1")) {
			String insertSubjectIndustry = "学术会议信息添加成功<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../conferenceAddPre.do");
			return mapping.findForward("info");
		} else {
			String insertSubjectIndustry = "学术会议信息添加失败<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../conferenceAddPre.do");
			return mapping.findForward("info");
		}
	}

}
